package com.tyler.controller;

import com.tyler.pojo.User;
import com.tyler.vo.Result;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

/**
 * @author: TylerZhong
 * @description:
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<Object> login(@RequestBody User user, HttpSession httpSession) {
        if (user == null || !"123".equals(user.getPassword())) {
            return Result.fail("用户名或密码错误");
        }
        httpSession.setAttribute("currentUser", user.getUsername());
        return Result.success();
    }

    /**
     * 获取用户名
     */
    @GetMapping("/getUsername")
    public Result<String> getUsername(HttpSession httpSession) {
        return Result.success(httpSession.getAttribute("currentUser").toString());
    }
}
